package com.spzx.product.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spzx.common.core.utils.StringUtils;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.product.domain.ProductUnit;
import com.spzx.product.service.ProductUnitService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * projectName: spzx-parent
 *
 * @author: 比丢比丢
 * time: 2024/7/4 9:54 周四
 * description:
 */

@Tag(name = "商品单位接口管理")
@RestController
@RequestMapping("/productUnit")
public class ProductUnitController extends BaseController {

    @Autowired
    private ProductUnitService productUnitService;

    //加载商品单位数据
    @Operation(summary = "查询商品单位列表")
    @GetMapping("getUnitAll")
    public AjaxResult selectProductUnitAll(){
        return success(productUnitService.list());
    }




    //添加商品单位
    @Operation(summary = "添加商品单位")
    @PutMapping("/add")
    public  AjaxResult add(@RequestBody ProductUnit productUnit){
        boolean save = productUnitService.save(productUnit);
        int rows = 0;
        if(save){
            rows = 1;
        }
        return toAjax(rows);
    }




    //修改商品单位
    @Operation(summary = "修改商品单位")
    @PutMapping("/update")
    public AjaxResult update(@RequestBody ProductUnit productUnit){
        boolean update = productUnitService.updateById(productUnit);
        int rows = 0;
        if(update){
            rows = 1;
        }
        return toAjax(rows);
    }




    //删除商品单位方法一  1,2,3
    @Operation(summary = "删除商品单位")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids){
        //批量删除
        List<Long> list = Arrays.asList(ids);
        productUnitService.removeBatchByIds(list);
        return toAjax(1);
    }
/*    //批量删除商品单位方法二
    @Operation(summary = "删除商品单位")
    @DeleteMapping("/remove")
    public AjaxResult removeBrandUnit(@RequestBody List<Long> ids){
        productUnitService.removeBatchByIds(ids);
        return toAjax(1);
    }*/




    //根据id查询接口
    @Operation(summary = "根据id查询接口")
    @GetMapping("/get/{id}")
    public AjaxResult get(@PathVariable Long id){
        ProductUnit productUnit = productUnitService.getById(id);
        return success(productUnit);
    }




    //分页条件查询接口
    @Operation(summary = "分页条件查询接口")
    @GetMapping("/list")
    public TableDataInfo list(
        @Parameter(name = "pageNum",description = "当前页码",required = true)
        @RequestParam(value = "pageNum",defaultValue = "0",required = true)
        Integer pageNum,

        @Parameter(name = "pageSize",description = "每页显示条数",required = true)
        @RequestParam(value = "pageSize",defaultValue = "10",required = true)
        Integer pageSize,

        @Parameter(name = "productUnit",description = "商品单位条件",required = false)
        ProductUnit productUnit){

        //1.创建page对象,传递当前页,每页显示记录数
        Page<ProductUnit> pageParam = new Page<ProductUnit>(pageNum, pageSize);

        //2.封装查询条件
        LambdaQueryWrapper<ProductUnit> wrapper = new LambdaQueryWrapper<>();

        //条件非空判断,不为空则进行条件选择
        if(StringUtils.hasText(productUnit.getName())){
            wrapper.eq(ProductUnit::getName,productUnit.getName());
        }

        //3.调用service方法实现条件分页查询(将page对象和条件选择器传递给service)
        IPage<ProductUnit> pageModel = productUnitService.page(pageParam,wrapper);

        //封装TableDataInfo返回值
//        TableDataInfo rspData = new TableDataInfo();
//        rspData.setCode(HttpStatus.SUCCESS);
//        rspData.setRows(pageModel.getRecords());
//        rspData.setMsg("查询成功");
//        rspData.setTotal(pageModel.getTotal());

        return getDataTable(pageModel);
    }

}
